//
//  LMSettingTableViewCell.m
//  彩票
//
//  Created by apple on 15/9/23.
//  Copyright (c) 2015年 李明. All rights reserved.
//

#import "LMSettingTableViewCell.h"
#import "LMSettingObject.h"

@interface LMSettingTableViewCell()

@property (nonatomic,strong)UIImageView *mImageView;

@property (nonatomic,strong)UISwitch *mSwitch;

@property (nonatomic,strong)UILabel *mlable;
@end

@implementation LMSettingTableViewCell


- (UIImageView *)mImageView
{
    if (_mImageView == nil) {
        _mImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];

    }
    return _mImageView;
}


- (UISwitch *)mSwitch
{
    if (_mSwitch == nil) {
        _mSwitch =[[UISwitch alloc] init];
        [_mSwitch addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    }
    return _mSwitch;
}

- (UILabel *)mlable
{
    if (_mlable == nil) {
        UILabel  *label= [[UILabel alloc] init];
        label.bounds = CGRectMake(0, 0, 80, 44);
        label.text = @"00:00";
        _mlable = label;
    }
    return _mlable;
}

- (void)changeValue:(UISwitch *)mSwitch
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:mSwitch.isOn forKey:self.object.title];
    [defaults synchronize];
    
    
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"settingCell";
    
    LMSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell==nil) {
        
        cell = [[LMSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }

    return cell;
}


- (void)setObject:(LMSettingObject *)object
{
    _object = object;
    
    self.textLabel.text = object.title;
    if (object.icon) {
           self.imageView.image = [UIImage imageNamed:object.icon];
    }

    self.detailTextLabel.text = object.subtitle;
    
    if ([object.type isEqual:@"1"]) {
        self.accessoryView = self.mImageView;
    }
    else if([object.type isEqual:@"0"])
    {
        
        self.mSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:object.title];
        self.accessoryView = self.mSwitch;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    else if([object.type isEqual:@"2"])
    {
        self.accessoryView = self.mlable;
    }

}
@end
